package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.order.OrderDetail;
import com.cskaoyan.gateway.form.order.OrderList;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import com.mall.user.intercepter.TokenIntercepter;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import java.util.Map;
import java.util.UUID;

/**
 * @author sisyphus
 * @date 2020/7/10 17:25
 * @desc 订单的增删改
 */
@Slf4j
@RestController
@RequestMapping("shopping")
public class OrderController {

    @Reference(timeout = 30000)
    private OrderCoreService orderCoreService;

    @Reference(timeout = 30000)
    private OrderQueryService orderQueryService;


    /**
     * 创建订单
     */
    @PostMapping("order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest order, HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        order.setUserId(uid);
        CreateOrderResponse response = orderCoreService.createOrder(order);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 获取用户当前订单
     */
    @GetMapping("order")
    public ResponseData listOrders(@Min(value = 1, message = "页数不能小于1") Integer page,
                                  @Min(value = 1, message = "每页条数不能小于1") Integer size,
                                  String sort, HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        OrderListRequest request = new OrderListRequest();
        request.setUserId(uid);
        request.setPage(page);
        request.setSize(size);
        request.setSort(sort);
        OrderListResponse response = orderQueryService.listOrders(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            OrderList listForm = new OrderList();
            listForm.setData(response.getDetailInfoList());
            listForm.setTotal(response.getTotal());
            return new ResponseUtil<>().setData(listForm);
        }
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 查询订单详情
     */
    @GetMapping("order/{id}")
    public ResponseData getOrderDetail(@PathVariable(name = "id") Long id) {
        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(String.valueOf(id));
        OrderDetailResponse response = orderQueryService.getOrderDetailById(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            OrderDetail detailForm = new OrderDetail();
            detailForm.setUserName(response.getBuyerNick());
            detailForm.setOrderTotal(response.getPayment());
            detailForm.setUserId(response.getUserId());
            detailForm.setGoodsList(response.getOrderItemDto());
            OrderShippingDto shippingDto = response.getOrderShippingDto();
            detailForm.setTel(shippingDto.getReceiverPhone());
            detailForm.setStreetName(shippingDto.getReceiverAddress());
            detailForm.setOrderStatus(response.getStatus());
            return new ResponseUtil<>().setData(detailForm);
        }
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 取消订单
     */
    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map map) {
        CancelOrderRequest request = new CancelOrderRequest();
        String orderId = (String) map.get("orderId");
        request.setOrderId(orderId);
        CancelOrderResponse response = orderCoreService.cancelOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("order/{id}")
    public ResponseData<String> orderDelete(@PathVariable String id) {
        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(id);
        DeleteOrderResponse response = orderCoreService.deleteOrder(request);
        return SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())
                ? new ResponseUtil<String>().setData(SysRetCodeConstants.SUCCESS.getMessage())
                : new ResponseUtil<String>().setErrorMsg(response.getMsg());
    }

}
